﻿' LibWowAPI
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.ObjectModel
Imports System.Linq
Imports Newtonsoft.Json
Imports roncliProductions.LibWowAPI.Data.CharacterRaces
Imports roncliProductions.LibWowAPI.Enums
Imports roncliProductions.LibWowAPI.Extensions

Namespace roncliProductions.LibWowAPI.Data.GuildRewards

    ''' <summary>
    ''' A class that retrieves guild reward information from the Blizzard WoW API.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class GuildRewards
        Inherits WowAPIData

        Private grRewards As New Schema.rewards

#Region "WowAPIData Overrides"

#Region "Public Properties"

        ''' <summary>
        ''' The length of time the data should be cached for, defaulting to 30 days for guild reward information.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Property CacheLength As New TimeSpan(30, 0, 0, 0)

#End Region

#Region "Protected Properties"

        Protected Overrides ReadOnly Property CacheKey As String
            Get
                Return "LibWowAPI.GuildRewards"
            End Get
        End Property

        Protected Overrides ReadOnly Property URI As Uri
            Get
                Return New Uri("/api/wow/data/guild/rewards", UriKind.Relative)
            End Get
        End Property

#End Region

#End Region

#Region "Properties"

        Private colRewards As Collection(Of Reward)
        ''' <summary>
        ''' A list of guild rewards as returned from the Blizzard WoW API.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Rewards As Collection(Of Reward)
            Get
                Return colRewards
            End Get
        End Property

#End Region

#Region "Methods"

#Region "Public"

        ''' <summary>
        ''' A default constructor to retrieve guild rewards information from the Blizzard WoW API.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            Load()
        End Sub

        ''' <summary>
        ''' Loads the guild rewards.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()
            grRewards = CType(JsonConvert.DeserializeObject(Data, GetType(Schema.rewards)), Schema.rewards)
            colRewards = (From r In grRewards.rewards
                          Select New Reward(
                              r.minGuildLevel,
                              CType(r.minGuildRepLevel, Standing),
                              GetRaces(r.races),
                              If(r.achievement Is Nothing, Nothing,
                                 New Achievement(
                                     r.achievement.id,
                                     r.achievement.title,
                                     r.achievement.points,
                                     r.achievement.description,
                                     r.achievement.reward,
                                     New Item(
                                         r.achievement.rewardItem.id,
                                         r.achievement.rewardItem.name,
                                         r.achievement.rewardItem.icon,
                                         CType(r.achievement.rewardItem.quality, Quality)
                                         )
                                     )
                                 ),
                             New Item(
                                 r.item.id,
                                 r.item.name,
                                 r.item.icon,
                                 CType(r.item.quality, Quality)
                                 )
                             )
                         ).ToCollection()
        End Sub

#End Region

#Region "Private"

        Private Shared Function GetRaces(intRaces As Collection(Of Integer)) As Collection(Of Race)
            If intRaces Is Nothing Then Return Nothing
            Dim crRaces As New CharacterRaces.CharacterRaces()
            Return (From r In crRaces.Races Where intRaces.Contains(r.ID)).ToCollection()
        End Function

#End Region

#End Region

    End Class

End Namespace
